package com.ipmirror.beans;

import com.ipmirror.common.base.AbstractBaseBean;
import com.ipmirror.component.PopupSelectCustomer;
import com.ipmirror.component.interfaces.IPopupSelectCustomer;
import com.ipmirror.ejb.Customer;
import com.ipmirror.rebuild.entities.MobileItem;
import com.ipmirror.rebuild.interfaces.ICustomerBean;
import com.ipmirror.rebuild.interfaces.IMobileItemBeanLocal;
import com.ipmirror.rebuild.view.PopupCustomerView;
import org.apache.log4j.Logger;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.event.ActionEvent;
import javax.faces.model.SelectItem;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by denny
 * User: denny
 * Date: Jan 12, 2011
 * Time: 5:24:37 PM
 */
public class MobileItemCreateBean extends AbstractBaseBean implements IPopupSelectCustomer{

    private static Logger log = Logger.getLogger(MobileItemCreateBean.class);
    private MobileItem newItem;
    private static List<SelectItem> statusList;
    private String managerName;
    private Integer managerPk;
    private PopupSelectCustomer popupSelectCustomer;
    private String billingContactName;
    private enum CustomerTyee {MANAGER, BILLING_CONTACT}
    private CustomerTyee popUpType = CustomerTyee.MANAGER;

    //Service
    @EJB(name = "IPMEjb/MobileItemBean/local")
    private IMobileItemBeanLocal mobileItemBean;
    @EJB(name = "IPMEjb/CustomerBean/local")
    private ICustomerBean customerService;
    @EJB(name = "IPMEjb/MobileItemBean/local")
    IMobileItemBeanLocal mobileItemService;

    @PostConstruct
    public void init() {
        log.info("init MobileItemBean.");
        newItem = new MobileItem();
    }

    private void renderPopupSelectCustomer(String title) {
        popupSelectCustomer.setRender(true);
        popupSelectCustomer.setTitle(title);
    }

    public String newCancel() {
        newItem=new MobileItem();
        return "";
    }

    public boolean validate(){
        return true;
    }

    public String newSave() {
        if(validate()){
            newItem.setCreatedBy("denliang");
            mobileItemService.create(newItem);
        }
        return "";
    }

    public MobileItem getNewItem() {
        return newItem;
    }

    public void setNewItem(MobileItem newItem) {
        this.newItem = newItem;
    }

    public void setStatusList(List<SelectItem> statusList) {
        this.statusList = statusList;
    }

    public List<SelectItem> getStatusList() {
        if (statusList == null) {
            List<String> status = mobileItemBean.showStatus();
            statusList = new ArrayList<SelectItem>(0);
            for (String str : status) {
                statusList.add(new SelectItem(str, str));
            }
        }
        return statusList;
    }

    public void setManagerName(String managerName) {
        this.managerName = managerName;
    }

    public String getManagerName() {
        return managerName;
    }

    public Integer getManagerPk() {
        return managerPk;
    }

    public void setManagerPk(Integer managerPk) {
        this.managerPk = managerPk;
    }

    public void selectMobileItemManager(ActionEvent actionEvent) {
        popUpType = CustomerTyee.MANAGER;
        renderPopupSelectCustomer("Select Manager");
    }

    public void selectMobileItemBillingContact(ActionEvent actionEvent) {
        popUpType = CustomerTyee.BILLING_CONTACT;
        renderPopupSelectCustomer("Select Billing Contact");
    }

    public PopupSelectCustomer getPopupSelectCustomer() {
        return popupSelectCustomer;
    }

    public void setPopupSelectCustomer(PopupSelectCustomer popupSelectCustomer) {
        this.popupSelectCustomer = popupSelectCustomer;
    }

    public void selectOk() {
        this.popupSelectCustomer.selectOk();
        PopupCustomerView selectedView=this.popupSelectCustomer.getSelectedCustomer();
        if(selectedView!=null){
            Customer customer= (Customer) customerService.find(selectedView.getPkCustomer());
            switch(popUpType){
                case MANAGER:
                    this.newItem.setManager(customer);
                    this.managerName=customer.getLoginName();
                    break;
                case BILLING_CONTACT:
                    this.newItem.setBillingContact(customer);
                    this.billingContactName=customer.getLoginName();
                    break;
            }
        }
    }

    public void closeOrCancelPopup() {
        this.popupSelectCustomer.closeOrCancelPopup();
    }

    public void setBillingContactName(String billingContactName) {
        this.billingContactName = billingContactName;
    }

    public String getBillingContactName() {
        return billingContactName;
    }
}
